﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BPM.Web;
using BPM.Web;
using BPM.Service;
using BPM.Entity;

namespace BPM.Controllers
{
	public class CategoryController : InternalViewController
	{
		public ActionResult CategoryList()
		{
			var allCates = new MaterialDefinitionCategoryService().GetAll()
				.GroupBy(c => c.ParentCategoryId).ToArray();
			var levelOneCates = allCates.FirstOrDefault(g => !g.Key.HasValue);
			if (levelOneCates != null)
			{
				this.ViewData["CategoryLevelOne"] = levelOneCates.ToArray();
			}

			this.ViewData["AllCates"] = allCates;

			return View();
		}

		public ActionResult AddCate(string name)
		{
			var svc = new MaterialDefinitionCategoryService();
			MaterialDefinitionCategory entity = null;
			var res = svc.Create(name, out entity);
			return Json(res);
		}

		public ActionResult AddSubCate(int bigCateId, string name)
		{
			var svc = new MaterialDefinitionCategoryService();
			MaterialDefinitionCategory entity = null;
			var res = svc.CreateSubCate(bigCateId, name, out entity);
			return Json(res);
		}

		public ActionResult EditCate(int id, string name)
		{
			var svc = new MaterialDefinitionCategoryService();
			var res = svc.ChangeName(id, name);
			return Json(res);
		}

		public ActionResult Delete(int id)
		{
			var svc = new MaterialDefinitionCategoryService();
			return Json(svc.Delete(id));
		}
	}

	public class CategoryControllerActions : ControllerDefinition<CategoryController>
	{
		public readonly string CategoryList = "CategoryList";
		public readonly string AddCate = "AddCate";
		public readonly string AddSubCate = "AddSubCate";
		public readonly string EditCate = "EditCate";
		public readonly string Delete = "Delete";
	}
}
